package com.qf.service.impl;

import com.qf.mapper.DeviceMapper;
import com.qf.pojo.Device;
import com.qf.service.DeviceService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    DeviceMapper deviceMapper;
    @Override
    public ResultMsg selectAll(int page,int limit) {
        List<Device> deviceList=deviceMapper.selectAll((page-1)*limit,limit);
        if (deviceList!=null){
            return ResultMsg.SUCCESS(0,deviceList,deviceList.size());
        }else{
            return ResultMsg.FAILED(201,"查询失败");
        }
    }

    @Override
    public ResultMsg selectOne(int deviceId) {
        Device device=deviceMapper.selectOne(deviceId);
        if (device!=null){
            return ResultMsg.SUCCESS(device);
        }else {
            return ResultMsg.FAILED(201,"查询错误");
        }
    }

    @Override
    public ResultMsg update(Device device) {
        if (deviceMapper.update(device)!=0){
            return ResultMsg.SUCCESS();
        }else {
            return ResultMsg.FAILED(201,"修改失败");
        }
    }

    @Override
    public ResultMsg insert(Device device) {
        if (deviceMapper.insert(device)!=0){
            return ResultMsg.SUCCESS();
        }else {
            return ResultMsg.FAILED(201,"新增失败");
        }
    }

    @Override
    public ResultMsg deleteOne(int deviceId) {
        if (deviceMapper.deleteOne(deviceId)!=0){
            return ResultMsg.SUCCESS();
        }else {
            return ResultMsg.FAILED(201,"删除失败");
        }
    }

    @Override
    public ResultMsg deleteMul(List<Integer> deviceIds) {
        if (deviceMapper.deleteMul(deviceIds)!=0){
            return ResultMsg.SUCCESS();
        }else {
            return ResultMsg.FAILED(201,"删除失败");
        }
    }

    @Override
    public ResultMsg selectDim(int page, int limit, String deviceName, Integer deviceStatus) {
        List<Device> deviceList2=deviceMapper.selectDim((page-1)*limit,limit,deviceName,deviceStatus);
        if (deviceList2!=null){
            return ResultMsg.SUCCESS(0,deviceList2,deviceList2.size());
        }else{
            return ResultMsg.FAILED(201,"查询失败");
        }
    }

    @Override
    public ResultMsg updateStatus(Integer deviceId, String deviceStatus) {
        if (deviceMapper.updateStatus(deviceId,deviceStatus)!=0){
            return ResultMsg.SUCCESS();
        }else {
            return ResultMsg.FAILED(201,"修改错误");
        }
    }

    @Override
    public ResultMsg updateStatusMul(List<Integer> deviceIds, Integer deviceStatus) {
        if (deviceMapper.updateStatusMul(deviceIds, deviceStatus) != 0) {
            return ResultMsg.SUCCESS();
        } else {
            return ResultMsg.FAILED(201, "查询错误");
        }
    }
}
